package com.woniuxy.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.dto.operations.CostRulesDetailDTO;
import com.woniuxy.entity.dto.operations.CostRulesDisplayDTO;
import com.woniuxy.entity.po.operations.BindCostAndTypeRules;
import com.woniuxy.entity.po.operations.ChargingStages;
import com.woniuxy.mapper.CostRulesMapper;
import com.woniuxy.service.CostRulesService;
import com.woniuxy.util.PageHelperUtil;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.TimeRuleValidatorUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.service
 * @Author: Quan
 * @Date: 2025/4/3 14:19
 */
@Service
@AllArgsConstructor
@Transactional
public class CostRulesServiceImpl implements CostRulesService {
	private CostRulesMapper costRulesMapper;
	private PageHelperUtil pageHelperUtil;

	/**
	 * @Description: 获取全部的收费阶段规则
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/3
	 */
	@Override
	public ResponseData<?> getChargingStage() {
		List<ChargingStages> chargingStages = costRulesMapper.queryAllChargingStages();
		return ResponseData.ok(chargingStages);
	}

	/**
	 * @Description: 将上述的描述，名称等内容加入cost_rules数据库中(并且要返回对应的cost_rules的id)，将List中的规则列表加入 bind_cost_and_type_rules中
	 * @Param: [costRulesDetailDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@Override
	public ResponseData<?> addCostRules(CostRulesDetailDTO costRulesDetailDTO) {
		// 同时将cost_rules中的id回显到costRulesDetailDTO中
		costRulesMapper.addCostRules(costRulesDetailDTO);
		// 将数据添加到bind_cost_and_type_rules中
		costRulesMapper.batchAddBindCostAndTypeRules(costRulesDetailDTO);
		return ResponseData.ok();
	}

	/** 
	 * @Description: 进行条件查询或者查询全部，并将查询到的bind_cost_and_type_rules中的内容转为计费时段与计费单价
	 * @Param: [pageParam]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@Override
	public ResponseData<?> queryAll(PageParam<CostRulesDisplayDTO> pageParam) {
		// 先根据条件查询到所有的规则数据
		PageInfo<CostRulesDetailDTO> pageInfo = pageHelperUtil.getPageInfo(pageParam, costRulesDisplayDTO -> costRulesMapper.queryAll(costRulesDisplayDTO));
		List<CostRulesDetailDTO> rulesDetailDTOList = pageInfo.getList();
		ArrayList<CostRulesDisplayDTO> displayDTOList = new ArrayList<>();

		for (CostRulesDetailDTO costRulesDetailDTO : rulesDetailDTOList) {
			costRulesDetailDTO.setBindCostAndTypeRulesList(TimeRuleValidatorUtil.sortRules(costRulesDetailDTO.getBindCostAndTypeRulesList()));
			CostRulesDisplayDTO displayDTO = new CostRulesDisplayDTO();
			displayDTO.setId(costRulesDetailDTO.getId());
			// rulesList中存放的是每一时间段的规则
			displayDTO.setCostName(costRulesDetailDTO.getCostName());
			displayDTO.setCostRulesCondition(costRulesDetailDTO.getCostRulesCondition());
			List<BindCostAndTypeRules> stageRuleList = costRulesDetailDTO.getBindCostAndTypeRulesList();
			StringBuilder costStageMsgTotal = new StringBuilder();
			StringBuilder costStageDetailTotal = new StringBuilder();
			for (BindCostAndTypeRules stageRule : stageRuleList) {
				String type = stageRule.getStageType();
				String costStageMsg = type + "(" + formatTime(stageRule.getStartTime()) + "~" + formatTime(stageRule.getEndTime()) + ")";
				String costStageDetail = type + "(￥" + stageRule.getStageCost() + ",￥" + stageRule.getStageServiceCost() + ")";
				costStageMsgTotal.append(costStageMsg);
				costStageDetailTotal.append(costStageDetail);
			}
			displayDTO.setCostStageMsg(costStageMsgTotal.toString());
			displayDTO.setCostStageDetail(costStageDetailTotal.toString());
			displayDTOList.add(displayDTO);
		}
		return ResponseData.ok(new PageInfo<>(displayDTOList));
	}


	private String formatTime(String time) {
		// 处理24:00的特殊情况
		if ("24:00".equals(time)) {
			return "24";
		}
		// 去除分钟部分（假设都是XX:00格式）
		return time.split(":")[0];
	}

	/**
	 * @Description: 通过对应的id对cost_rule以及阶段计费细节进行查看
 	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@Override
	public ResponseData<?> queryById(Long id) {
		return ResponseData.ok(costRulesMapper.queryById(id));
	}

	/**
	 * @Description: 通过id删除cost_rule和 收费规则细节表
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@Override
	public ResponseData<?> deleteById(Long id) {
		// 将对应的code_rule进行逻辑删除
		costRulesMapper.deleteCostRuleById(id);
		// 将bind_cost_and_type_rules中对应的 cost_rules_id = id的进行逻辑删
		costRulesMapper.deleteBindCostAndTypeRules(id);
		return ResponseData.ok();
	}

	/**
	 * @Description: 更新消费规则的状态（禁用=》启用，启用=》禁用）
	 * @Param: [costRulesDetailDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@Override
	public ResponseData<?> updateCostRulesCondition(CostRulesDetailDTO costRulesDetailDTO) {
		costRulesMapper.updateCostRulesCondition(costRulesDetailDTO);
		return ResponseData.ok();
	}


	@Override
	public ResponseData<?> updateCostRules(CostRulesDetailDTO costRulesDetailDTO) {
		costRulesMapper.updateCostRules(costRulesDetailDTO);
		return ResponseData.ok();
	}


}
